using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using Web.Core;

namespace AsmxData
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class UsersHandler : System.Web.Services.WebService
    {
        /// 
        [WebMethod]
        public string getList(string injson)
        {
		    JsonFormat jsf = new JsonFormat(injson);
		    if (!Authority.CheckAuthOnAsmx("UsersHandler.getList",jsf.GetValue("authid"), jsf.GetValue("authstr"), true))
			  return "{\"result\":\"-1\"}";
            int pagesize = Int16.Parse(jsf.GetValue("rows"));
            int currentpage = Int16.Parse(jsf.GetValue("page"));
            PageInfo pi = new PageInfo();
            pi.PageSize = pagesize;
            pi.PageIndex = currentpage;
    		Model.Users mdl = new Model.Users();
        
            PageList<Model.Users> lst = new BLL.Users().GetPageList(pi, mdl);
            string json = "{\"total\":" + pi.RecordCount.ToString()+",\"rows";
            json = Newtonsoft.Json.JavaScriptConvert.SerializeObject(lst).Replace("{\"List", json);
			return json;
        }

        /// 
        [WebMethod]
        public string getOne(string injson)
        {
		    JsonFormat jsf = new JsonFormat(injson);
		    if (!Authority.CheckAuthOnAsmx("UsersHandler.getOne",jsf.GetValue("authid"), jsf.GetValue("authstr"), true))
			  return "{\"result\":\"-1\"}";
            int? id = BLL.BLHelper.GetInt(jsf.GetValue("id"));
	    	Model.Users wheremdl = new Model.Users();
    		wheremdl.UserId = id;
            Model.Users mdl = new BLL.Users().GetModel(wheremdl);
            string json = "\"result\":{0},\"model\":{1}";
            if (mdl == null)
                json = String.Format(json, 0, "{}");
            else
                json = String.Format(json, 1, Newtonsoft.Json.JavaScriptConvert.SerializeObject(mdl));
            return "{" + json + "}";
        }

        /// 
        [WebMethod]
        public string AddOne(string injson)
        {
		    JsonFormat jsf = new JsonFormat(injson);
		    if (!Authority.CheckAuthOnAsmx("UsersHandler.AddOne",jsf.GetValue("authid"), jsf.GetValue("authstr"), true))
			  return "{\"result\":\"-1\"}";
            Model.Users mdl = new Model.Users();
	
            mdl.Account = BLL.BLHelper.GetString(jsf.GetValue("Account"));
            mdl.Pass = BLL.BLHelper.GetString(jsf.GetValue("Pass"));
            mdl.TrueName = BLL.BLHelper.GetString(jsf.GetValue("TrueName"));
            mdl.UserSex = BLL.BLHelper.GetBool(jsf.GetValue("UserSex"));
            mdl.Question = BLL.BLHelper.GetString(jsf.GetValue("Question"));
            mdl.Answer = BLL.BLHelper.GetString(jsf.GetValue("Answer"));
            mdl.BirthDay = BLL.BLHelper.GetString(jsf.GetValue("BirthDay"));
            mdl.QQ = BLL.BLHelper.GetString(jsf.GetValue("QQ"));
            mdl.Msn = BLL.BLHelper.GetString(jsf.GetValue("Msn"));
            mdl.GUID = BLL.BLHelper.GetString(jsf.GetValue("GUID"));
            mdl.IsClose = BLL.BLHelper.GetBool(jsf.GetValue("IsClose"));
            mdl.Point = BLL.BLHelper.GetInt(jsf.GetValue("Point"));
            mdl.LastIP = BLL.BLHelper.GetString(jsf.GetValue("LastIP"));
            mdl.LastTime = BLL.BLHelper.GetDateTime(jsf.GetValue("LastTime"));
            mdl.RegIP = BLL.BLHelper.GetString(jsf.GetValue("RegIP"));
            mdl.RegTime = BLL.BLHelper.GetDateTime(jsf.GetValue("RegTime"));
            new BLL.Users().Add(mdl);
            return "{\"result\":\"1\"}";
        }

        /// 
        [WebMethod]
        public string RemoveOne(string injson)
        {
		    JsonFormat jsf = new JsonFormat(injson);
		    if (!Authority.CheckAuthOnAsmx("UsersHandler.RemoveOne",jsf.GetValue("authid"), jsf.GetValue("authstr"), true))
			  return "{\"result\":\"-1\"}";
            int? id = BLL.BLHelper.GetInt(jsf.GetValue("id"));
            Model.Users wheremdl = new Model.Users();
	    	wheremdl.UserId = id;
	    	new BLL.Users().Delete(wheremdl);
	    	return "{\"result\":\"1\"}";
        }

        /// 
        [WebMethod]
        public string UpdataOne(string injson)
        {
		    JsonFormat jsf = new JsonFormat(injson);
		    if (!Authority.CheckAuthOnAsmx("UsersHandler.UpdataOne",jsf.GetValue("authid"), jsf.GetValue("authstr"), true))
			  return "{\"result\":\"-1\"}";
            Model.Users setmdl = new Model.Users();
	    	Model.Users wheremdl = new Model.Users();
	
            setmdl.UserId = BLL.BLHelper.GetInt(jsf.GetValue("UserId"));
            setmdl.Account = BLL.BLHelper.GetString(jsf.GetValue("Account"));
            setmdl.Pass = BLL.BLHelper.GetString(jsf.GetValue("Pass"));
            setmdl.TrueName = BLL.BLHelper.GetString(jsf.GetValue("TrueName"));
            setmdl.UserSex = BLL.BLHelper.GetBool(jsf.GetValue("UserSex"));
            setmdl.Question = BLL.BLHelper.GetString(jsf.GetValue("Question"));
            setmdl.Answer = BLL.BLHelper.GetString(jsf.GetValue("Answer"));
            setmdl.BirthDay = BLL.BLHelper.GetString(jsf.GetValue("BirthDay"));
            setmdl.QQ = BLL.BLHelper.GetString(jsf.GetValue("QQ"));
            setmdl.Msn = BLL.BLHelper.GetString(jsf.GetValue("Msn"));
            setmdl.GUID = BLL.BLHelper.GetString(jsf.GetValue("GUID"));
            setmdl.IsClose = BLL.BLHelper.GetBool(jsf.GetValue("IsClose"));
            setmdl.Point = BLL.BLHelper.GetInt(jsf.GetValue("Point"));
            setmdl.LastIP = BLL.BLHelper.GetString(jsf.GetValue("LastIP"));
            setmdl.LastTime = BLL.BLHelper.GetDateTime(jsf.GetValue("LastTime"));
            setmdl.RegIP = BLL.BLHelper.GetString(jsf.GetValue("RegIP"));
            setmdl.RegTime = BLL.BLHelper.GetDateTime(jsf.GetValue("RegTime"));

            new BLL.Users().Update(setmdl, wheremdl);
            return "{\"result\":\"1\"}";
        }
    }
}
  